package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ReturnGoodsEntity;
import cn.mumzone.pos.ui.widgets.shopping.AnimShopButton;

/**
 * Created by JoeLjt on 2019/7/9.
 * Email: lijiateng1219@gmail.com
 * Description:
 * <p>
 * 退货流程第一步，选择退货商品的 adapter
 */

public class ADA_BillReturnCartStepOne extends CommonAdapter<ReturnGoodsEntity> {

    public ADA_BillReturnCartStepOne(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_return_cart_step_one;
    }

    @Override
    protected void convert(ViewHolder holder, final ReturnGoodsEntity entity, final int position) {

        holder.setText(R.id.tv_cart_good_name, entity.getGoodsName());

        holder.setText(R.id.tv_cart_good_price, String.format("￥%s", entity.getRealPay()));

        holder.setText(R.id.tv_cart_good_spec, getGoodSpecString(entity.getGoodsSpec(), entity.getUnitName()));

        if (entity.getConsignNum() == 0) {
            // 没有做过寄存
            holder.setText(R.id.tv_goods_count_not_register, String.format("购买数量：%s", entity.getGoodsNum()));
            holder.setVisible(R.id.ll_not_register_layout, true);
            holder.setVisible(R.id.ll_register_layout, false);

        } else {

            if (entity.getConsignNum() - entity.getSendNum() == entity.getGoodsNum()) {
                // 是否全部寄存
                holder.setVisible(R.id.ll_not_register_layout, false);
                holder.setVisible(R.id.ll_register_layout, true);
                holder.setText(R.id.tv_goods_count_register, String.format("寄存数量：%s", entity.getConsignNum()));
            } else {
                holder.setVisible(R.id.ll_not_register_layout, true);
                holder.setVisible(R.id.ll_register_layout, true);
                holder.setText(R.id.tv_goods_count_not_register, String.format("未寄存数量：%s", entity.getGoodsNum() - (entity.getConsignNum() - entity.getSendNum())));
                holder.setText(R.id.tv_goods_count_register, String.format("寄存数量：%s", entity.getConsignNum() - entity.getSendNum()));
            }

        }

        AnimShopButton vNotRegister = holder.getView(R.id.v_not_register);
        vNotRegister.setCount(entity.getSelectUnRegisterCount());
        // 区分商品有无寄存
        vNotRegister.setMinCount(entity.getSelectRegisterCount() > 0 ? 0 : 1);
        vNotRegister.setMaxCount(entity.getGoodsNum() - (entity.getConsignNum() - entity.getSendNum()));
        vNotRegister.setOnAddDelListener(new GoodsReturnButtonAdapter(mContext) {
            @Override
            protected void onAmountChanged(int count) {
                entity.setSelectUnRegisterCount(count);
                entity.setSelectCount(entity.getSelectUnRegisterCount() + entity.getSelectRegisterCount());
                if (mListener != null) {
                    mListener.onAmountChanged(position);
                }
            }

            @Override
            public void onAddFailed(int count, FailType failType) {
                String toastMsg = "退货数量不能大于购买数量~";
                if (Integer.parseInt(entity.getRegisterCount()) > 0) {
                    toastMsg = "退货数量不能大于未寄存数量~";
                }
                PosToast.INSTANCE.makeShortToast(mContext, toastMsg);
            }

        });

        AnimShopButton vRegister = holder.getView(R.id.v_register);
        vRegister.setCount(entity.getSelectRegisterCount());
        vRegister.setMinCount(entity.getSelectUnRegisterCount() > 0 ? 0 : 1);
        vRegister.setMaxCount(entity.getConsignNum() - entity.getSendNum());
        vRegister.setOnAddDelListener(new GoodsReturnButtonAdapter(mContext) {
            @Override
            protected void onAmountChanged(int count) {
                entity.setSelectRegisterCount(count);
                entity.setSelectCount(entity.getSelectUnRegisterCount() + entity.getSelectRegisterCount());
                if (mListener != null) {
                    mListener.onAmountChanged(position);
                }
            }

            @Override
            public void onAddFailed(int count, FailType failType) {
                PosToast.INSTANCE.makeShortToast(mContext, "寄存退货数量不能大于寄存数量~");
            }

        });

        holder.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onGoodsDeleted(position);
                }
            }
        });

    }

    public interface OnGoodsAmountChangedListener {
        void onAmountChanged(int position);

        void onGoodsDeleted(int position);
    }

    private OnGoodsAmountChangedListener mListener;

    public void setOnGoodsAmountChangedListener(OnGoodsAmountChangedListener mListener) {
        this.mListener = mListener;
    }

    private String getGoodSpecString(List<ReturnGoodsEntity.GoodsSpecBean> goodsSpec, String unitName) {
        StringBuilder builder = new StringBuilder();
        builder.append("规格：");
        if (!ListUtil.isListEmpty(goodsSpec) || !TextUtils.isEmpty(unitName)) {
            for (int i = 0; i < goodsSpec.size(); i++) {
                ReturnGoodsEntity.GoodsSpecBean goodsSpecBean = goodsSpec.get(i);
                builder.append(goodsSpecBean.getValue());
                builder.append(",");
            }
            builder.append(unitName);
        } else {
            builder.append("暂无");
        }
        return builder.toString();
    }
}
